package com.ws.core.advice;

import com.ws.common.exception.BaseRuntimeException;
import com.ws.common.util.ResponseResult;
import com.ws.common.util.ResultCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by WS on 2018/1/26.
 */
@ControllerAdvice
public class ExceptionHandleAdvice {

    @ResponseBody
    @ExceptionHandler({BaseRuntimeException.class})
    public ResponseResult handleBaseRuntimeException(HttpServletRequest request, HttpServletResponse response, RuntimeException e){
        if (e instanceof BaseRuntimeException){
            BaseRuntimeException baseRuntimeException = (BaseRuntimeException) e;
            return new ResponseResult(baseRuntimeException.getCode(),baseRuntimeException.getMsgParams()[0]);
        }
        return new ResponseResult(ResultCode.Common.RUNTIME_EXCEPTION);
    }


}
